package com.lizx.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

public class WechatUtils {

    public static String appid = "wx7189c41e3dbc1d17";
    public static String appsecret = "387735cd7b6c7c4fde04826a21c41c18";
    public static String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    public static String authorizeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";

    public static String scopeSnsapiBase = "snsapi_base";

    public static String scopeSnsapiUserinfo  = "snsapi_userinfo";

    public static String codeUrl = "https://www.krwl.top/staff/employee/getCode";

    public static String getOpenIdUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";


    public String getAccessToken(){
        String accessTokenUrl = this.accessTokenUrl.replace("APPID",WechatUtils.appid)
                .replace("APPSECRET",WechatUtils.appsecret);
        HttpClient client = new HttpClient(accessTokenUrl);
        try {
            client.get();
            String content = client.getContent();
            JSONObject jsonObject = JSON.parseObject(content);
            String access_token = (String) jsonObject.get("access_token");
            return access_token;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public void authorize(){
        String authorizeUrl = this.authorizeUrl.replace("APPID",WechatUtils.appid)
                .replace("REDIRECT_URI", URLEncoder.encode(WechatUtils.codeUrl)).replace("SCOPE",WechatUtils.scopeSnsapiBase);
        HttpClient client = new HttpClient(authorizeUrl);
        try {
            client.get();
            String content = client.getContent();
            System.out.println("public void authorize======"+content);
            JSONObject jsonObject = JSON.parseObject(content);
            System.out.println("JSONObject jsonObject==="+jsonObject);
            String access_token = (String) jsonObject.get("access_token");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public String getOpenId(String code){
        String getOpenIdUrl = this.getOpenIdUrl.replace("APPID",WechatUtils.appid)
                .replace("SECRET",WechatUtils.appsecret).replace("CODE",code);
        System.out.println("--------------getOpenIdUrl---------------");
        System.out.println(getOpenIdUrl);
        System.out.println("--getOpenIdUrlgetOpenIdUrlgetOpenIdUrlgetOpenIdUrl----");
        HttpClient client = new HttpClient(getOpenIdUrl);
        try {
            client.get();
            String content = client.getContent();
            System.out.println("======================");
            System.out.println(content);
            System.out.println("---------------");
            JSONObject jsonObject = JSON.parseObject(content);
            String openid = (String) jsonObject.get("openid");
            return openid;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {

    }

    public void setTemplateMsg(){

    }

}
